import { xy2expr, DataProxy, Validation, Validator } from '@yuto/x-spreadsheet'

export class CustomDataProxy extends DataProxy {
  constructor (name, settings) {
    super(name, settings)
    this.styles = [
      {
        bgcolor: '#f0f0f0'
      },
      {
        bgcolor: '#FDE3E4'
      },
      {
        bgcolor: '#f9f9f9',
        color: '#999'
      }
    ]

    this.validations._ = []
    this.numberValid = {
      type: 'number',
      required: false,
      operator: 'gte',
      value: 0
    }
    this.dateValid = {
      type: 'date',
      required: true
    }
    this.validations._[0] = new Validation('cell', [], this.getValidator(this.numberValid))
    this.validations._[1] = new Validation('cell', [], this.getValidator(this.dateValid))
    this.validations._[0].refSet = new Set()
    this.validations._[1].refSet = new Set()
  }

  getValidator ({
    type,
    required,
    operator,
    value
  }) {
    return new Validator(type, required, value, operator)
  }

  mergeRange (range, rn, cn) {
    const { rows } = this
    if (rn > 1 || cn > 1) {
      const { sri, sci } = range
        const cell = rows.getCellOrNew(sri, sci)
        cell.merge = [rn - 1, cn - 1]
        cell.editable = false
        // delete merge cells
        this.rows.deleteCells(range)
        // console.log('cell:', cell, this.d);
        this.rows.setCell(sri, sci, cell)
    }
  }

  renderCell (ri, ci, { value, editable, style, validator, width, clickable }) {
    if (style === undefined && !value) return
    var cell = this.rows.getCellOrNew(ri, ci)
    if (cell) {
      cell.editable = true
      cell.text = value || ''
      if (this.editable === false) {
        cell.editable = false
        cell.style = 2
      } else {
        cell.editable = editable
        cell.style = style
      }
    }
    // xy2expr(ci, ri)
    const ref = xy2expr(ci, ri)
    this.setCellValidator(ref, validator)
    if (width) {
      this.cols.setWidth(ci, width)
    }
    if (clickable) {
      this.addCustomClickRef(ref)
    }
  }

  getCell (ri, ci) {
    return this.rows.getCell(ri, ci)
  }

  getCellText (ri, ci) {
    var cell = this.rows.getCell(ri, ci)
    if (cell) return cell.text || ''
    else return ''
  }

  setCellEditable (ri, ci, editable = true) {
    var cell = this.rows.getCell(ri, ci)
    if (cell) cell.editable = editable
  }

  setCellStyle (ri, ci, style) {
    var cell = this.rows.getCell(ri, ci)
    if (cell) cell.style = style
  }

  addCellValidator (ref, validator) {
    if (validator === 'number') {
      if (this.validations._[0].refSet.has(ref)) return
      this.validations._[0].refSet.add(ref)
      this.validations._[0].refs.push(ref)
    }
    if (validator === 'date') {
      if (this.validations._[0].refSet.has(ref)) return
      this.validations._[1].refSet.add(ref)
      this.validations._[1].refs.push(ref)
    }
  }

  setCellValidator (ref, validator) {
    if (validator === 'number') {
      this.validations._[0].refSet.add(ref)
      this.validations._[0].refs.push(ref)
    }
    if (validator === 'date') {
      this.validations._[1].refSet.add(ref)
      this.validations._[1].refs.push(ref)
    }
  }
}
